<template>
  <div>
    <van-search
      v-model.trim="word"
      show-action
      label="商品"
      background="#f66"
      placeholder="请输入搜索关键词"
      @search="onSearch"
    >
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search>
    <div class="wrap">
      <h5>搜索记录</h5>
      <div class="histories">
        <div class="his" v-for="his in histories" :key="his">
          {{his}}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      word: '',
      histories: localStorage.getItem('his') ? JSON.parse(localStorage.getItem('his')) : []
    }
  },
  methods: {
    onSearch () {
      // 点击搜索
      // 处理历史记录
      let his = localStorage.getItem('his') ? JSON.parse(localStorage.getItem('his')) : []
      his.unshift(this.word)
      his = [...new Set(his)]
      localStorage.setItem('his', JSON.stringify(his))
      this.$router.push({
        path: '/searchLists',
        query: {
          keyWord: this.word
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.histories{
  display: flex;
  flex-wrap: wrap;
  .his{
    padding: 8px;
    background-color: #f6f6f6;
    margin-left: 10px;
    margin-top:5px;
  }
}
</style>
